/*
 * This file Copyright (c) 2015. Walle.
 * (http://www.wallellen.com). All rights reserved.
 *
 *
 * This file is dual-licensed under both the
 * Walle Agreement (WA) and the GNU General Public License.
 * You may elect to use one or the other of these licenses.
 *
 * This file is distributed in the hope that it will be
 * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
 * implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
 * Redistribution, except as permitted by whichever of the GPL
 * or WA you select, is prohibited.
 *
 * 1. For the GPL license (GPL), you can redistribute and/or
 * modify this file under the terms of the GNU General
 * Public License, Version 3, as published by the Free Software
 * Foundation.  You should have received a copy of the GNU
 * General Public License, Version 3 along with this program;
 * if not, write to the Free Software Foundation, Inc., 51
 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * 2. For the Walle Agreement (WA), this file
 * and the accompanying materials are made available under the
 * terms of the WA which accompanies this distribution, and
 * is available at http://www.wallellen.com/agreement.html
 *
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
 */

package com.wallellen.core.page;

import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by walle on 12/9/15.
 */
public class ViewMode {
    private Map<String, Object> attrs = new HashMap();

    public static ViewMode getInstance() {
        ViewMode obj = new ViewMode();
        obj.attrs.put("STATE", Integer.valueOf(StateType.SUCCESS.value));
        obj.attrs.put("OPERATE", Integer.valueOf(OperateType.OTHER.value));
        return obj;
    }

    public ViewMode putAttr(String key, String value) {
        this.attrs.put(key, value);
        return this;
    }

    public ViewMode putAttr(String key, Object value) {
        this.attrs.put(key, value);
        return this;
    }

    public ViewMode putAttrs(Map<String, Object> map) {
        this.attrs.putAll(map);
        return this;
    }

    public ViewMode setError(String message) {
        this.attrs.put("MESSAGE", message);
        this.attrs.put("STATE", Integer.valueOf(StateType.ERROR.value));
        return this;
    }

    public ViewMode setOperate(OperateType type) {
        this.attrs.put("OPERATE", Integer.valueOf(type.value));
        return this;
    }

    public Map<String, Object> returnObjMode() {
        return this.attrs;
    }

    public ModelAndView returnModelAndView(String path) {
        return new ModelAndView(path, returnObjMode());
    }

    public ModelAndView returnRedirectUrl(String path) {
        String paras = null;
        for (String name : this.attrs.keySet()) {
            String val = this.attrs.get(name).toString();
            if (paras == null)
                paras = name + "=" + val;
            else {
                paras = paras + "&" + name + "=" + val;
            }
        }
        if (paras == null) {
            paras = "";
        }
        if (path.indexOf("?") > 0) {
            path = path + "&" + paras;
        } else if (!paras.equals("")) {
            path = path + "?" + paras;
        }

        return new ModelAndView("redirect:" + path);
    }

    @Deprecated
    public String returnStrMode() {
        return "String";
    }

    public static List<?> returnListObjMode(List<?> list) {
        return list;
    }
}
